<template>
  <BasicModal title="复制"
    :width="500"
    :canFullscreen="false"
    :centered="true"
    :destroyOnClose="true"
    @register="registerModal"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" :model="model" />
  </BasicModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref, reactive } from "vue";
import { BasicModal, useModalInner } from "/@/components/Modal";
import { BasicForm, useForm } from "/@/components/Form/index";
import { copy, queryById } from "/@/views/weixin/article/artticle.api";
import { select } from "/@/views/weixin/menu/menu.api";
import { useMessage } from "/@/hooks/web/useMessage";

const $message = useMessage();

// 声明Emits
const emit = defineEmits(['success', 'register']);

//表单modal
const model = reactive<any>({})

//表单配置
const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
  schemas: [
    { label: "", field: "id", component: "Input", show: false },
    { label: "所属栏目", field: "menuentityId", component: 'ApiTreeSelect', required: true,
      componentProps: {
        api: select,
        fieldNames: { children: 'children', label: 'name', key: 'id', value: 'id', selectable: 'selectable' },
        dropdownStyle: {
          maxHeight: '50vh',
        },
        treeDefaultExpandAll:true,
        getPopupContainer: () => document.body,
      },
    },
    { label: "编号", field: "number", component: "Input",
      rules: [
        { required: true },
        { pattern: /^[A-Z][0-9][0-9]/, message: '产品编号不正确，应以大写字母+两位数字；（如：L14）。' }
      ]
    },
    { label: "副标题", field: "sub", component: "Input",
      rules: [
        { required: true },
        { pattern:  /[\u4e00-\u9fa5A-Za-z0-9_]/, message: '不能包含特殊字符！' }
      ]
    },
    { label: "主标题", field: "title", component: "Input", required: true },
    { label: "行程天数", field: "days", component: "InputNumber", required: true, componentProps: { placeholder: " " } },
    { label: "截止天数", field: "isGuestEnd", component: "InputNumber", required: true, componentProps: { placeholder: " " } },
    { label: "排序", field: "orders", component: "InputNumber", componentProps: { placeholder: " " } },
  ],
  labelWidth: 90,
  baseColProps: {span: 22},
  showActionButtonGroup: false,
});

const formEl = ref();
const isUpdate = ref(true);
//表单赋值
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  setModalProps({confirmLoading: false});
  isUpdate.value = !!data?.isUpdate;
  await resetFields()
  if (isUpdate.value) {
    await queryById({id: data?.id}).then((res) => {
      if (res.success) {
        setFieldsValue(res.result)
      }
    });
  }
});
//标题
const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

//表单提交事件
async function handleSubmit() {
  try {
    setModalProps({confirmLoading: true});
    const values = await validate();
    await copy(values).then((res) => {
      if (res.success) {
        $message.createMessage.success(res.message);
        //关闭弹窗
        closeModal();
        emit("success")
      } else {
        $message.createMessage.warning(res.message);
      }
    });
  } catch (e) {
    console.log(e);
  } finally {
    setModalProps({confirmLoading: false});
  }
}

</script>

<style lang="less" scoped>

</style>
